//
//  ViewController.m
//  09-provinceCityZone-03
//
//  Created by 402030900@qq.com on 16/7/26.
//  Copyright © 2016年 402030900@qq.com. All rights reserved.
//


#import "ViewController.h"
#import "zoneTableViewController.h"

@interface ViewController ()
@property (nonatomic,strong) NSMutableArray *array;
@property (nonatomic, strong) NSMutableArray *proArr;
@property (nonatomic, strong) NSMutableArray *cityArr;
@end

@implementation ViewController

- (void)viewDidLoad {
    
    self.navigationItem.title = @"省市区";
      [self.tableView registerClass:[UITableViewCell class] forCellReuseIdentifier:@"cell"];
    self.proArr = [NSMutableArray array];
    self.cityArr = [NSMutableArray array];
    NSString *path = @"/Users/402030900qq.com/DLS160710/UI/HomeWork/09-provinceCityZone-03/09-provinceCityZone-03/area.txt";
    NSString *strSource = [NSString stringWithContentsOfFile:path encoding:NSUTF8StringEncoding error:nil];
    NSArray *mainArray = [strSource componentsSeparatedByString:@"\n"];
    self.array = [NSMutableArray array];
    for (NSString *string in mainArray) {
        if (![string hasPrefix:@" "])
        {
            NSMutableDictionary *dicOfProvince = [NSMutableDictionary dictionary];
            [dicOfProvince setValue:string forKey:@"provinceName"];
            [self.array addObject:dicOfProvince];
            NSMutableArray *arrOfCity = [NSMutableArray array];
            [dicOfProvince setValue:arrOfCity forKey:@"cityArray"];
        }
        if ([string hasPrefix:@"  "] && ![string hasPrefix:@"    "]) {
            NSMutableDictionary *dicOfProvince = [self.array lastObject];
            NSMutableArray *arrayOfCity = [dicOfProvince objectForKey:@"cityArray"];
            NSMutableDictionary *dicOfCity = [NSMutableDictionary dictionary];
            [dicOfCity setValue:string forKey:@"cityName"];
            [arrayOfCity addObject:dicOfCity];
            NSMutableArray *arrOfArea = [NSMutableArray array];
            [dicOfCity setValue:arrOfArea forKey:@"zoneArray"];
        }
        if ([string hasPrefix:@"    "]) {
            NSMutableDictionary *dicOfProvince = [self.array lastObject];
            NSMutableArray *arrayOfCity = [dicOfProvince objectForKey:@"cityArray"];
            NSMutableDictionary *dicOfCity = [arrayOfCity lastObject];
            NSMutableArray *arrayOfArea = [dicOfCity objectForKey:@"zoneArray"];
            [arrayOfArea addObject:string];
        }
    }
}
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
    return self.array.count;
}
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    NSDictionary *dic = [_array objectAtIndex:section];
    NSArray *arr = dic[@"cityArray"];
    return arr.count;
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"cell"];
    NSString *province = _array[indexPath.section][@"cityArray"][indexPath.row][@"cityName"];
    cell.textLabel.text = province;
    return cell;
}
- (NSString *)tableView:(UITableView *)tableView titleForHeaderInSection:(NSInteger)section
{
    
    return _array[section][@"provinceName"];
}
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    zoneTableViewController *area = [[zoneTableViewController alloc] init];
    area.zoneArr = _array[indexPath.section][@"cityArray"][indexPath.row][@"zoneArray"];
    [self.navigationController pushViewController:area animated:YES];
}
- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

@end
